package com.cn.ant.modules.wechat.service;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.service.BaseService;
import com.cn.ant.modules.wechat.dao.TextTemplateMapper;
import com.cn.ant.modules.wechat.entity.TextTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文本消息模板Service
 * @author AntDream
 * @version 2015-03-22
 */
@Component
@Transactional(readOnly = true)
public class TextTemplateService extends BaseService {

	@Autowired
	private TextTemplateMapper textTemplateMapper;
	
	public TextTemplate get(String id) {
		return textTemplateMapper.get(id);
	}
	
	public Page<TextTemplate> find(Page<TextTemplate> page, Map<String,Object> param) {
        param.put("startIdx", page.getFirstResult());
        param.put("pageSize", page.getPageSize());
        page.setList(textTemplateMapper.query(param));
        page.setCount(textTemplateMapper.queryCount(param));
        return page;
	}

    public TextTemplate findByTemplateId(String templateId) {
        Map<String, Object> param = new HashMap<>();
        param.put("templateId", templateId);
        List<TextTemplate> templateList = textTemplateMapper.query(param);
        if (templateList != null && templateList.size() > 0) {
            return templateList.get(0);
        }
        return  null;
    }

    public List<TextTemplate> queryForList(Map<String,Object> param) {
        return textTemplateMapper.query(param);
    }

	@Transactional(readOnly = false)
	public void save(TextTemplate textTemplate) {
		textTemplateMapper.insert(textTemplate);
	}
	
	@Transactional(readOnly = false)
	public void update(TextTemplate textTemplate) {
		textTemplateMapper.update(textTemplate);
	}
	
	@Transactional(readOnly = false)
	public void delete(String id) {
		textTemplateMapper.delete(id);
	}
	
}
